import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.memorandum import Memorandum
from utils.manager import Images


class Test:
    app = Memorandum
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('预置条件.清除测试数据')
        device.hdc_shell('rm -rf /data/app/el2/100/database/com.ohos.note/default/rdb/note.db')
        device.wait(2)
        logging.info('步骤1.进入桌面，点击备忘录应用，进入笔记，点击右上角☆按钮')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        travel = device.get_element_by_text('旅游心得')
        device.click_element(travel)
        device.wait(2)
        favor_img0 = device.save_snapshot_to_local('favor_img0.jpeg')
        Images.crop_picture(favor_img0, *self.app.favor_area)
        x1, y1, x2, y2 = self.app.favor_area
        similarity = Images.compare_image_similarity(favor_img0, self.app.unfavored_pic)
        # 相似度大于80%，说明未收藏
        if similarity > 0.6:
            device.click((x1 + x2) // 2, (y1 + y2) // 2)
            device.wait(2)
        logging.info('预期结果1.五角星⭐被点亮')
        favor_img1 = device.save_snapshot_to_local('favor_img1.jpeg')
        Images.crop_picture(favor_img1, *self.app.favor_area)
        similarity = Images.compare_image_similarity(favor_img1, self.app.favored_pic)
        assert similarity > 0.6, '收藏图标未被点亮'

        logging.info('步骤2.在收缩文件夹-我的收藏查看')
        device.go_back()
        device.click(*self.app.open_menu)
        device.click(*self.app.my_favor)
        device.click(*self.app.close_menu)
        logging.info('预期结果2.我的收藏中笔记数量相应增加，笔记被添加进入我的收藏')
        device.refresh_layout()
        device.assert_text_exist('旅游心得')

        logging.info('步骤3.取消收藏，查看结果')
        device.click_element(device.get_element_by_text('旅游心得'))
        device.wait(1)
        device.click((x1 + x2) // 2, (y1 + y2) // 2)
        device.wait(1)
        device.go_back()
        logging.info('预期结果3.笔记中再次点击五角星，☆不亮，我的收藏笔记数量相应减少，笔记被移除我的收藏')
        device.refresh_layout()
        device.assert_text_exist('旅游心得',expect=False)
        device.click(*self.app.open_menu)
        device.click(*self.app.all_note)
        device.click(*self.app.close_menu)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('旅游心得'))
        device.wait(2)
        favor_img2 = device.save_snapshot_to_local('favor_img2.jpeg')
        Images.crop_picture(favor_img2, *self.app.favor_area)
        similarity = Images.compare_image_similarity(favor_img0, self.app.unfavored_pic)
        assert similarity > 0.6, '收藏图标点亮效果未取消'
